<?php
	require_once $_SERVER['DOCUMENT_ROOT'] . '/SAGER/model/interfaces/IBillDetailAccessDao.php';
	require_once $_SERVER['DOCUMENT_ROOT'] . '/SAGER/model/interfaces/IConnectionAccessDao.php';
	require_once $_SERVER['DOCUMENT_ROOT'] . '/SAGER/model/factory/ConnectionFactory.php';
	
	error_reporting (E_ALL ^ E_NOTICE);
	
	$debug = $_SESSION['debug'];

	class MySqlBillDetailAccessDao implements IBillDetailAccessDao {
  		/**
		 * Constructor of the bill detail dao
		 */
	 	public function __construct () {}
		
		/**
		 * Creates a bill detail
		 */
     	public function createBillDetail($billDetail) {
			$pkLine = $billDetail -> getPkLine();
			$units = $billDetail -> getUnits();
			$quantity = $billDetail -> getQuantity();
			$unitCost = $billDetail -> getUnitCost();
			$pkFkNumberBill = $billDetail -> getPkFkNumberBill();
			$pkFkControlNumberBill = $billDetail -> getPkFkControlNumberBill();
			$pkFkCodeProductInventory = $billDetail -> getPkFkCodeProductInventory();
			$pkFkRifBranchInventory = $billDetail -> getPkFkRifBranchInventory();
			
			$connection = ConnectionFactory :: getMySqlConnectionAccessDao();
			$conn = $connection -> openConnection();
			
			$query = "INSERT INTO BILL_DETAIL VALUES ($pkLine, '$units', $quantity, $unitCost, 
				$pkFkNumberBill, $pkFkControlNumberBill, $pkFkCodeProductInventory, 
				'$pkFkRifBranchInventory')";
			
			$GLOBALS['debug'] -> log($query, "Query");
			
			$sentence = mysql_query($query, $conn);
			
			if ($sentence)
				return true;
			
			return false;
	 	}
			/**
		 * Selects all Bill details
		 */
		public function selectBillDetails($number, $controlNumber) {
			$connection = ConnectionFactory :: getMySqlConnectionAccessDao();
			$conn = $connection -> openConnection();
		
			
			$sentence = mysql_query($query, $conn);
			
			$query = "SELECT rd.pk_line, CONCAT( p.name, ' (', p.specifications, ')' ) name, 
					  rd.units, rd.quantity, rd.unit_cost, rd.quantity * rd.unit_cost total,r.tax
					  FROM BILL r, BILL_DETAIL rd, PRODUCT p
					  WHERE r.pk_number = $number
					  AND r.pk_control_number = $controlNumber
					  AND r.pk_number = rd.pk_fk_number_bill
					  AND r.pk_control_number = rd.pk_fk_control_number_bill
					  AND rd.pk_fk_code_product_inventory = p.pk_code";
			
			$sentence = mysql_query($query, $conn);
				
			$conn = $connection -> closeConnection($conn);
			
			return $sentence;
		 }
		

		
		
	}
?>